package ferp.core.ai.filter;

import ferp.core.card.Card;
import ferp.core.game.Game;
import ferp.core.player.Hand;

import java.util.Arrays;

/**
 * User: igorgok
 * Date: 11/25/12 5:27 PM
 */
public class FilterSecondPlusKingTrick
{
  // the method is called for player that is not declarer, has hand 0 and for first 4 rounds only
  public static int apply(int candidates, int player, int playerCards, int declarer, int declarerCards)
  {
    // clear the second plus king suits array
    Arrays.fill(suits, null);
    int size = 0;

    // determine if declarer has second plus king
    for (Card.Suit s : Card.Suit.rising)
      if (Hand.only(candidates, s) != 0)
      {
        int cards = Hand.get(declarerCards, s);
        int length = Card.Suit.length(cards);

        //Log.log.debug(Log.TAG, "Checking second+ king for suit " + s + ", cards " + cards);

        // "second king plus" is defined as 2-3 cards long suit with no ace, with king and without marriage or triliage
        if (!Hand.contains(cards, Card.Mask.ACE) && Hand.contains(cards, Card.Mask.KING) &&
            ((length == 2 && !Hand.contains(cards, Card.Mask.QUEEN)) ||
             (length == 3 && (!Hand.contains(cards, Card.Mask.QUEEN) || !Hand.contains(cards, Card.Mask.JACK)))))
          suits[size++] = s;
      }

    // if there is no second plus king in any suit - return all candidates
    if (size == 0)
      return candidates;

    // the declarer's hand is 1
    if (Game.next(player) == declarer)
    {
      for (int i = 0; i < size; ++i)
      {
        int cards = Hand.get(playerCards, suits[i]);

        // if the player's hand is 0 and declarer is 1 and player has no ACE then all candidates in this suit are valid
        if (!Hand.contains(cards, Card.Mask.ACE))
          suits[i] = null;
      }
    }

    int filtered = candidates;

    // all remaining suit sets should be removed from candidates as they can make the second plus king to take a trick
    for (int i = 0; i < size; ++i)
    {
      Card.Suit s = suits[i];

      if (s != null)
        filtered = Hand.remove(filtered, Hand.only(filtered, s));
    }

    if (filtered == 0)
      return candidates;

    return filtered;
  }

  // array containing second plus king suits
  private final static Card.Suit[] suits = new Card.Suit[Card.Suit.rising.length];

/*
  public static void main(String[] argv)
  {
    int cards = 0xA8;
    int length = Card.Suit.length(cards);

    boolean b = (!Hand.contains(cards, Card.Mask.ACE) && Hand.contains(cards, Card.Mask.KING) &&
          ((length == 2 && !Hand.contains(cards, Card.Mask.QUEEN)) ||
           (length == 3 && (!Hand.contains(cards, Card.Mask.QUEEN) || !Hand.contains(cards, Card.Mask.JACK)))));

    System.out.println(b + ", " + length);

    File parent = new File("D:\\Test");

    b = parent.canRead();
    b = parent.canWrite();

    File f = new File(parent, "Oleg");
    File[] files = f.listFiles();

    b = f.mkdir();
    files = f.listFiles();

    System.out.println(b);
  }
*/
}
